﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.FlowProcess;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.FlowProcess;
using System.Xml;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(ICompleteFlowNodeService))]
    internal class CompleteFlowNodeService : ICompleteFlowNodeService
    {
        [Import]
        protected IFlowRepository FlowRepository { get; set; }
        [Import]
        protected IFlowNodeRepository FlowNodeRepository { get; set; }
        [Import]
        protected ICompleteFlowNodeRepository CompleteFlowNodeRepository { get; set; }

        public List<CompleteFlowNodeView> GetCompleteFlowNode(string recordID)
        {
            List<CompleteFlowNodeView> completeFlowNodeViewList = new List<CompleteFlowNodeView>();
            IQueryable<CompleteFlowNode> completeFlowNodeList = CompleteFlowNodeRepository.Entities.Where(c => c.RecordID == recordID);
            foreach (CompleteFlowNode completeFlowNode in completeFlowNodeList)
            {
                completeFlowNodeViewList.Add(new CompleteFlowNodeView()
                {
                    ID = completeFlowNode.ID,
                    RecordID = completeFlowNode.RecordID,
                    NodeCode = completeFlowNode.NodeCode,
                    NodeName = completeFlowNode.NodeName,
                    NodeProcessDepartmentPerson = completeFlowNode.NodeProcessDepartmentPerson,
                    PreNodeCode = completeFlowNode.PreNodeCode,
                    ParentNodeCode = completeFlowNode.ParentNodeCode,
                    NodeLevel = completeFlowNode.NodeLevel,
                    NodeType = completeFlowNode.NodeType,
                    NodeSubType = completeFlowNode.NodeSubType,
                    SpecState = completeFlowNode.SpecState
                });
            }
            return completeFlowNodeViewList;
        }

        public List<CompleteFlowNodeView> GetCompleteFlowNodeByParentNodeCode(string recordID, string parentNodeCode, bool isContrainParent, bool isDesc)
        {
            List<CompleteFlowNodeView> completeFlowNodeViewList = new List<CompleteFlowNodeView>();
            IQueryable<CompleteFlowNode> completeFlowNodeList = null;
            if (isContrainParent)
            {
                if (isDesc)
                {
                    completeFlowNodeList = CompleteFlowNodeRepository.Entities.Where(c => c.RecordID == recordID && (c.ParentNodeCode == parentNodeCode || c.NodeCode == parentNodeCode)).OrderByDescending(c => c.AddDate);
                }
                else
                {
                    completeFlowNodeList = CompleteFlowNodeRepository.Entities.Where(c => c.RecordID == recordID && (c.ParentNodeCode == parentNodeCode || c.NodeCode == parentNodeCode)).OrderBy(c => c.AddDate);
                }
            }
            else
            {
                completeFlowNodeList = CompleteFlowNodeRepository.Entities.Where(c => c.RecordID == recordID && c.ParentNodeCode == parentNodeCode).OrderByDescending(c => c.AddDate);
            }
            foreach (CompleteFlowNode completeFlowNode in completeFlowNodeList)
            {
                completeFlowNodeViewList.Add(new CompleteFlowNodeView()
                {
                    ID = completeFlowNode.ID,
                    RecordID = completeFlowNode.RecordID,
                    NodeCode = completeFlowNode.NodeCode,
                    NodeName = completeFlowNode.NodeName,
                    NodeProcessDepartmentPerson = completeFlowNode.NodeProcessDepartmentPerson,
                    PreNodeCode = completeFlowNode.PreNodeCode,
                    ParentNodeCode = completeFlowNode.ParentNodeCode,
                    NodeLevel = completeFlowNode.NodeLevel,
                    NodeType = completeFlowNode.NodeType,
                    NodeSubType = completeFlowNode.NodeSubType,
                    SpecState = completeFlowNode.SpecState
                });
            }
            return completeFlowNodeViewList;
        }

        public List<CompleteFlowNodeView> GetCompleteFlowNodeByNodeCode(string recordID, string nodeCode)
        {
            List<CompleteFlowNodeView> completeFlowNodeViewList = new List<CompleteFlowNodeView>();
            IQueryable<CompleteFlowNode> completeFlowNodeList = CompleteFlowNodeRepository.Entities.Where(c => c.RecordID == recordID && c.NodeCode == nodeCode);
            foreach (CompleteFlowNode completeFlowNode in completeFlowNodeList)
            {
                completeFlowNodeViewList.Add(new CompleteFlowNodeView()
                {
                    ID = completeFlowNode.ID,
                    RecordID = completeFlowNode.RecordID,
                    NodeCode = completeFlowNode.NodeCode,
                    NodeName = completeFlowNode.NodeName,
                    NodeProcessDepartmentPerson = completeFlowNode.NodeProcessDepartmentPerson,
                    PreNodeCode = completeFlowNode.PreNodeCode,
                    ParentNodeCode = completeFlowNode.ParentNodeCode,
                    NodeLevel = completeFlowNode.NodeLevel,
                    NodeType = completeFlowNode.NodeType,
                    NodeSubType = completeFlowNode.NodeSubType,
                    SpecState = completeFlowNode.SpecState
                });
            }
            return completeFlowNodeViewList;
        }

        public void InsetCompleteFlowNode(List<CompleteFlowNodeView> addList)
        {
            foreach (CompleteFlowNodeView completeFlowNodeView in addList)
            {
                CompleteFlowNode existCompleteFlowNode = CompleteFlowNodeRepository.Entities.SingleOrDefault(c => c.ID == completeFlowNodeView.ID);
                if (existCompleteFlowNode == null)
                {
                    CompleteFlowNode completeFlowNode = new CompleteFlowNode()
                    {
                        ID = completeFlowNodeView.ID,
                        RecordID = completeFlowNodeView.RecordID,
                        NodeCode = completeFlowNodeView.NodeCode,
                        NodeName = completeFlowNodeView.NodeName,
                        NodeProcessDepartmentPerson = completeFlowNodeView.NodeProcessDepartmentPerson,
                        PreNodeCode = completeFlowNodeView.PreNodeCode,
                        ParentNodeCode = completeFlowNodeView.ParentNodeCode,
                        NodeLevel = completeFlowNodeView.NodeLevel,
                        NodeType = completeFlowNodeView.NodeType,
                        NodeSubType = completeFlowNodeView.NodeSubType,
                        SpecState = completeFlowNodeView.SpecState
                    };
                    CompleteFlowNodeRepository.Insert(completeFlowNode);
                }
            }
        }

        public void UpdateCompleteFlowNode(List<CompleteFlowNodeView> updateList)
        {
            foreach (CompleteFlowNodeView completeFlowNodeView in updateList)
            {
                CompleteFlowNode existCompleteFlowNode = CompleteFlowNodeRepository.Entities.SingleOrDefault(c => c.ID == completeFlowNodeView.ID);
                if (existCompleteFlowNode != null)
                {
                    existCompleteFlowNode.SpecState = completeFlowNodeView.SpecState;
                    CompleteFlowNodeRepository.Update(existCompleteFlowNode);
                }
            }
        }

        public void DeleteCompleteFlowNode(List<CompleteFlowNodeView> deleteList)
        {
            foreach (CompleteFlowNodeView completeFlowNodeView in deleteList)
            {
                CompleteFlowNodeRepository.Delete(completeFlowNodeView.ID);
            }
        }
    }
}